﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using Stud.DTOs;
using Stud.BusinessFacade;

public partial class Student : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var userinfo = Membership.GetUser();
        lblUser.Text = "hello " + userinfo.Email;
       
        if (!Page.IsPostBack)
        {
           List<StudentInfo> stList=new List<StudentInfo>();
            stList.Add(new StudentInfo{RollNo=1,Name="Arun",Age=21});
            stList.Add(new StudentInfo{RollNo=2,Name="Karun",Age=21});
            stList.Add(new StudentInfo{RollNo=2,Name="Sunil",Age=21});
            dlstStateCutoffTime.DataSource = stList;
            dlstStateCutoffTime.DataBind();

            grdStudents.DataSource = new StudentFacade().GetAllStudents();
            grdStudents.DataBind();
        }
        grdStudents.RowCommand += new GridViewCommandEventHandler(grdStudents_RowCommand);
        grdStudents.RowDeleting += new GridViewDeleteEventHandler(grdStudents_RowDeleting);
        grdStudents.RowDataBound += new GridViewRowEventHandler(grdStudents_RowDataBound);
        grdStudents.RowDeleted += new GridViewDeletedEventHandler(grdStudents_RowDeleted);
        grdStudents.PageIndexChanging += new GridViewPageEventHandler(grdStudents_PageIndexChanging);

        //btnSave.Click += new EventHandler(btnSave_Click);
        //btnSave.Click += new EventHandler(btnSave_Click2);

        //StudentFacade facade = new StudentFacade();
        //var sts = facade.GetAllStudents();
        //grdStudents.DataSource = sts;
        //grdStudents.DataBind();
    }

    void grdStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdStudents.PageIndex = e.NewPageIndex;
        grdStudents.DataSource = new StudentFacade().GetAllStudents();
        grdStudents.DataBind();
    }

    void grdStudents_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        throw new NotImplementedException();
    }
    protected void btnSave_Click2(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        List<StudentInfo> lst=new List<StudentInfo>();

        foreach (DataListItem item in dlstStateCutoffTime.Items)
        {
            Label lblStateName = (Label) item.FindControl("lblStateName") as Label;
            TextBox txt1 = (TextBox) item.FindControl("txt1") as TextBox;

            lst.Add(new StudentInfo{Name=lblStateName.Text,Age = Convert.ToInt32(txt1.Text)});
        }


        StudentInfo student = new StudentInfo
        {
            RollNo = Convert.ToInt64(txtRoll.Text),
            Name = txtName.Text,
            Age = Convert.ToInt32(txtAge.Text)
        };
        //StudentFacade facade = new StudentFacade();
        //facade.SaveStudent(student);

        var success = new StudentFacade().SaveStudent(student);
        if (success)
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "key", "javascript:alert('Data Added.');", true);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "key", "javascript:alert('Error in Data Add.');", true);
        }
    }


    void grdStudents_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lnkbtnDel = (LinkButton)e.Row.FindControl("lnkBtnDelete");
            if (lnkbtnDel != null)
            {
                lnkbtnDel.Attributes.Add("onclick", "javascript:return confirm('Are you sure that you want to delete this Record?')");
            }
        }
    }

    void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        //var b = grdStudents.DataKeys[e.RowIndex].Value;
        //new StudentFacade().DeleteByRollNo(Convert.ToInt64(grdStudents.DataKeys[e.RowIndex].Value));

    }

    void grdStudents_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("Delete"))
        {
            new StudentFacade().DeleteByRollNo(Convert.ToInt64(e.CommandArgument));
            grdStudents.DataSource = new StudentFacade().GetAllStudents();
            grdStudents.DataBind();
        }
        if (e.CommandName.Equals("Go"))
        {
            //Response.Redirect("~/StudInfo.aspx?rno=" + e.CommandArgument.ToString());
            var stud = new StudentFacade().GetStudentByRollNo(Convert.ToInt64(e.CommandArgument));
            lblName.Text = stud.Name;
            mpeEdit.Show();
        }
    }
    public string GetGrade(object objAge)
    {
        int age = Convert.ToInt32(objAge);
        string grd = string.Empty;
        if (age > 40)
            grd = "Sr. Student";
        else
            grd = "Jr. Student";
        return grd;
    }
}